package com.sphd.emanu.activity.marketing;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.activity.dis.DisActivity;
import com.sphd.emanu.activity.discountcoupon.DiscountcouponActivity;
import com.sphd.emanu.activity.integralscore.IntegralscoreActivity;
import com.sphd.emanu.activity.takeout.TakeoutActivity;
import com.sphd.emanu.activity.topupservice.TopupserviceActivity;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.market.Config;
import com.sphd.emanu.pojo.market.Coupon;
import com.sphd.emanu.pojo.market.Deposit;
import com.sphd.emanu.pojo.market.Discount;
import com.sphd.emanu.pojo.market.Points;
import com.sphd.emanu.pojo.market.Rules;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 * 营销模块的备注
 * @Author Hu
 * @Time 2017/5/10 -- 17:26.
 * @Email 87683202@qq.com
 */
public class MarketingActivity extends MVPBaseActivity<MarketingContract.View, MarketingPresenter> implements MarketingContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_take_out)
    TextView tvTakeOut;
    @BindView(R.id.layout_take_out)
    LinearLayout layoutTakeOut;
    @BindView(R.id.tv_discount_coupon)
    TextView tvDiscountCoupon;
    @BindView(R.id.layout_discount_coupon)
    LinearLayout layoutDiscountCoupon;
    @BindView(R.id.tv_top_up)
    TextView tvTopUp;
    @BindView(R.id.layout_top_up)
    LinearLayout layoutTopUp;
    @BindView(R.id.tv_dis_msg)
    TextView tvDisMsg;
    @BindView(R.id.layout_dis_msg)
    LinearLayout layoutDisMsg;
    @BindView(R.id.layout_integral_score)
    LinearLayout layoutIntegralScore;
    Intent intent;
    Bundle bundle;
    User user;
    Rules rules;
    @BindView(R.id.out_msg)
    TextView outMsg;
    @BindView(R.id.coupon_msg)
    TextView couponMsg;
    @BindView(R.id.topu_msg)
    TextView topuMsg;
    @BindView(R.id.dis_msg)
    TextView disMsg;
    @BindView(R.id.points_msg)
    TextView pointsMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marketing);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void initView() {
        setTitleBar(toolbar, "营销");
        intent = getIntent();
        bundle = intent.getExtras();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        addData();
    }

    private void addData() {
        showProgressDialog();
        mPresenter.selectRules(user.getOid());
    }

    @Override
    public void setRules(Rules rules) {
        dissProgressDialog();
        this.rules = rules;
        Config config = rules.getConfig();
        tvTakeOut.setText(config.getIsOutside() ? "开启" : "未开启");
        tvDiscountCoupon.setText(config.getIsCoupon() ? "开启" : "未开启");
        tvTopUp.setText(config.getIsDeposit() ? "开启" : "未开启");
        tvDisMsg.setText(config.getIsDiscount() ? "开启" : "未开启");
        if (config.getIsOutside()){
            outMsg.setText("送餐时间：" + config.getEarlyTime() + "分钟，送餐范围：" + config.getDistance() + "米，送餐费：" + config.getFee() +
                    ",起送价格" + config.getAmountLimit());

        }
         if (config.getIsCoupon())  {
             Coupon coupon = rules.getCoupon();
             couponMsg.setText("消费" + coupon.getThreshold() + "元以上可获得一张优惠券，面值：" + coupon.getCoupon() + "元，满" + coupon.getConditions() +
                     "元可以使用，有效期" + coupon.getExpireDays() + "天");
         }
        if (config.getIsDeposit()){
            List<Deposit> depositList = rules.getDeposit();
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < depositList.size(); i++) {
                Deposit deposit = depositList.get(i);
                stringBuffer.append("缴费" + deposit.getThreshold() + "充值" + deposit.getThreshold() + "\t");
            }
            topuMsg.setText(TextUtils.isEmpty(stringBuffer.toString()) ? "暂无" : stringBuffer.toString());
        }
        if (config.getIsDiscount()){
            Discount discount = rules.getDiscount();
            disMsg.setText("会员折扣" + (discount.getDiscountRate()/10) + (discount.getAdvanceRate() == 0 ? "" : ",支持提前下单" + (
                    discount.getAdvanceRate()/10) + "折"));
        }
        List<Points> pointsList = rules.getPoints();
        StringBuffer stringBufferPoint = new StringBuffer();
        for (int i = 0; i < pointsList.size(); i++) {
            Points points = pointsList.get(i);
            if (points.getEnable()) {
                stringBufferPoint.append((i == 0 ? "充值" : i == 1 ? "消费充值卡金额" : "现金消费" )+
                        points.getAmount() + ",给" + points.getPonits() + "积分\t");
            }
        }
        pointsMsg.setText(stringBufferPoint.toString());


    }

    @OnClick({R.id.layout_take_out, R.id.layout_discount_coupon, R.id.layout_top_up, R.id.layout_dis_msg, R.id.layout_integral_score})
    public void onViewClicked(View view) {
        bundle.putSerializable(Constant.RULES, rules);
        intent.putExtras(bundle);
        switch (view.getId()) {
            case R.id.layout_take_out:
                //外卖服务
                intent.setClass(context, TakeoutActivity.class);
                startActivityForResult(intent, Constant.REQUEST_CODE);
                break;
            case R.id.layout_discount_coupon:
                //优惠券服务
                intent.setClass(context, DiscountcouponActivity.class);
                startActivityForResult(intent, Constant.REQUEST_CODE);
                break;
            case R.id.layout_top_up:
                //充值服务
                intent.setClass(context, TopupserviceActivity.class);
                startActivityForResult(intent, Constant.REQUEST_CODE);
                break;
            case R.id.layout_dis_msg:
                //折扣率
                intent.setClass(context, DisActivity.class);
                startActivityForResult(intent, Constant.REQUEST_CODE);
                break;
            case R.id.layout_integral_score:
                //积分设置
                intent.setClass(context, IntegralscoreActivity.class);
                startActivityForResult(intent, Constant.REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUEST_CODE) {
            addData();
        }
    }
}
